using Gateway.IQuery;
using Gateway.IQuery.WorkFlow;
using Gateway.IQuery.WorkFlow.Request;
using Gateway.IQuery.WorkFlow.Result;
using Gateway.IRepositories;
using SqlSugar;

namespace Gateway.Query;

/// <summary>
/// 
/// </summary>
public class WorkFlowQuery : IWorkFlowQuery
{
    private readonly IQueryRepository _queryRepository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="queryRepository"></param>
    public WorkFlowQuery(IQueryRepository queryRepository)
    {
        _queryRepository = queryRepository;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<PageResult<WorkFlowListResult>> GetList(WorkFlowPageListRequest request)
    {
        RefAsync<int> count = 0;
        var data = await _queryRepository.Db.Queryable<WorkFlowDetailResult>()
            .Where(x => (string.IsNullOrEmpty(request.WorkFlowName) || x.WorkFlowName.Contains(request.WorkFlowName))
                        && (string.IsNullOrEmpty(request.WorkFlowCode) || x.WorkFlowCode.Equals(request.WorkFlowCode))
            ).ToPageListAsync(request.Page, request.PageSize, count);

        return PageResult<WorkFlowListResult>.Create(data.Select(p => new WorkFlowListResult()
        {
            WorkFlowId = p.WorkFlowId,
            WorkFlowName = p.WorkFlowName,
            WorkFlowCode = p.WorkFlowCode,
            CreatedTime = p.CreatedTime,
            LastModifiedTime = p.LastModifiedTime,
            Disabled = p.Disabled
        }).ToList(), count);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<WorkFlowDetailResult> GetDetail(string id)
    {
        var data = await _queryRepository.FindAsync<WorkFlowDetailResult>(x => x.WorkFlowId.Equals(id));
        return data;
    }
}